<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="18">
          <el-input placeholder="分类id" v-model="searchModel.id" clearable> </el-input>
          
          <el-button @click="getTypeList" type="primary" icon="el-icon-search" round>查询</el-button>
        </el-col>
        <el-col :span="6" align="right">
          <el-button @click="openEditUI(null)" type="primary" icon="el-icon-plus" circle></el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 结果列表 -->
    <el-card>
      <el-table :data="typeList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            {{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="类型编号" width="180">
        </el-table-column>
        <el-table-column prop="typeName" label="类型名称" width="180">
        </el-table-column>
  
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button @click="openEditUI(scope.row.id)" type="primary" icon="el-icon-edit" circle
              size="mini"></el-button>
            <el-button @click="deleteType(scope.row)" type="danger" icon="el-icon-delete" circle size="mini"></el-button>
            <el-button @click="goBack()" type="primary">返回</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo" :page-sizes="[5, 10, 20, 50]" :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <!-- 对话框 -->
    <el-dialog @close="clearForm" :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="typeForm" ref="typeFormRef" :rules="rules">
        <el-form-item prop="typeName" label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="typeForm.typeName" autocomplete="off"></el-input>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveType">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import typeManage from '@/api/typeManage'
export default {
  data(){
    
    return{
      formLabelWidth: '130px',
      typeForm: {},
      dialogFormVisible: false,
      title: '',
      searchModel: {
        pageNo: 1,
        pageSize: 10
      },
      typeList: [],
      total: 0,
      rules:{
        typeName: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    goBack() {
      this.searchModel.typeName = ''; // 清空用户名搜索条件
      this.searchModel.id = '';
      this.searchModel.pageNo = 1;

      // 返回操作逻辑
      this.getTypeList();
    },
    deleteType(type){
      this.$confirm(`您确定删除分类 ${type.typeName} ？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        typeManage.deleteTypeById(type.id).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          });
          this.dialogFormVisible = false;
          this.getTypeList();
        });
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    saveType(){
      // 触发表单验证
      this.$refs.typeFormRef.validate((valid) => {
        if (valid) {
          // 提交保存请求
          typeManage.saveType(this.typeForm).then(response => {
            // 成功提示
            this.$message({
              message: response.message,
              type: 'success'
            });
            // 关闭对话框
            this.dialogFormVisible = false;
            // 刷新表格数据
            this.getTypeList();
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    clearForm(){
      this.typeForm = {};
      this.$refs.typeFormRef.clearValidate();
    },
    openEditUI(id){
      if(id == null){
        this.title = '新增分类';
      }else{
        this.title = '修改分类';
        typeManage.getTypeById(id).then(response => {
          this.typeForm = response.data;
        });
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize){
      this.searchModel.pageSize = pageSize;
      this.getTypeList();
    },
    handleCurrentChange(pageNo){
      this.searchModel.pageNo = pageNo;
      this.getTypeList();
    },
    getTypeList(){
      typeManage.getTypeList(this.searchModel).then(response => {
        this.typeList = response.data.rows;
        this.total = response.data.total;
      });
    }
  },
  created(){
    this.getTypeList();
  }
};
</script>
  
<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 85%;
}
</style>